package config2excel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.ufgov.smartclient.component.table.JGroupableTable;

import config2excel.export.DataExporter;
import config2excel.resolve.ConfigResolveProxy;
import config2excel.resolve.IConfigResolve;
import config2excel.ui.C2ETableModel;
import config2excel.util.StringUtil;

public class MainClass extends JFrame {

  private static final long serialVersionUID = -1752408232349644072L;

  public JFileChooser chooser; // 文件选择器

  private JButton export, fileChoose;

  private IConfigResolve resolve;

  private C2ETableModel tableModel;

  private JGroupableTable table;

  public JPanel mainPanel;

  private String FilePath = "E:/fgt_system19.4.conf";

  public List list = new ArrayList();

  // 定义一维数据作为列标题
  String[] columnTitle = { "name", "interface", "type", "subnet" };

  String[] ColumnTitleDisplays = { "名称", "接口", "类型", "地址" };

  List columnList = new ArrayList();

  public MainClass() {
    // initData();

    chooser = new JFileChooser(); // 初始化文件选择器
    chooser.setCurrentDirectory(new File("d:\\"));// 文件选择器的初始目录定为d盘

    mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    tableModel = new C2ETableModel(columnTitle, ColumnTitleDisplays);
    table = new JGroupableTable();
    tableModel.setData(list);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
    JToolBar toolbar = new JToolBar();

    export = new JButton("导出excel");
    export.addActionListener(getExportExcelAction());

    fileChoose = new JButton("选择文件");
    fileChoose.addActionListener(getFileChooseAction());

    toolbar.add(fileChoose);
    toolbar.add(export);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(table);
    table.setModel(tableModel);

    this.setTitle("导出配置文件");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    mainPanel.add(scrollPane, BorderLayout.CENTER);
    mainPanel.add(toolbar, BorderLayout.NORTH);
    this.getContentPane().add(mainPanel);
    toolbar.setFloatable(false);
    // this.setLocation(200, 200);
    this.setSize(1000, 800);
    this.setVisible(true);
    this.pack();

  }

  public void initData() {

    FileReader fr = null;
    BufferedReader br = null;
    try {
      try {
        fr = new FileReader(FilePath);
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      }
      br = new BufferedReader(new InputStreamReader(new FileInputStream(FilePath),
        "gb2312"));
      String Line = br.readLine();
      while (Line != null) {
        if (StringUtil.isStrBegin(Line.trim(), "config")) {
          resolve = ConfigResolveProxy.getConfigResolve(Line.trim());
        }
        if (resolve != null) {
          resolve.ResolveConfig(Line);
        }
        if ("end".equals(Line.trim())) {
          resolve = null;
        }
        Line = br.readLine();
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (br != null)
          br.close();
        if (fr != null)
          fr.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    list.addAll(resolve.addressList);
    tableModel.setData(list);
  }

  private ActionListener getExportExcelAction() {
    return new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {

        boolean flag = true;
        if (table.getCheckedRows().length == 0) {
          if (JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(
            MainClass.this, "没有选择导出的数据，是否全部导出？", "提示", JOptionPane.YES_NO_OPTION))
            flag = false;
        }
        if (flag)
          DataExporter.exportToExcel(MainClass.this.mainPanel, table, table
            .getCheckedRows());
        // new GroupableTableDataExport(table).execute(true);
      }
    };
  }

  private ActionListener getFileChooseAction() {
    return new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        chooser.setFileSelectionMode(0);// 设定只能选择到文件
        int state = chooser.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
        if (state == 1) {
          return;// 撤销则返回
        } else {
          File f = chooser.getSelectedFile();// f为选择到的文件
          FilePath = f.getAbsolutePath();
          System.out.println("FilePath=" + FilePath);
          // tableModel.setData(null);
          list.removeAll(list);
          resolve.addressList.clear();
          initData();
        }
      }
    };
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
          e.printStackTrace();
        }
        new MainClass();
      }
    });
    // JFrame frame = new JFrame();
    // frame.setTitle("Test");
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // frame.setLocation(200, 200);
    // frame.setSize(300, 300);
    // frame.setVisible(true);
  }
}
